//
//  BaseViewController.m
//  SmartCar
//
//  Created by cslab on 16/4/2.
//  Copyright © 2016年 cslab. All rights reserved.
//

#import "BaseViewController.h"
#import <UIScrollView+EmptyDataSet.h>
#import "UIView+Gradient.h"
@interface BaseViewController () <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong) UIImageView *navBGImageView;

@property (nonatomic, copy) NSString *refreshDescribe;
@property (nonatomic, copy) NSString *refreshBtnName;
@property (nonatomic, copy) NSString *refreshImageName;

@property (nonatomic, copy) void (^ refreshBlck) ();

@end

@implementation BaseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.barBGAlpha = 1;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:self.navBGImageView];
    
    
//    [self backBarButtonWithImageName:@"back"];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    if (self.navigationController.viewControllers.count > 1) {
        self.sideMenuController.leftViewSwipeGestureEnabled = NO;
    } else {
        self.sideMenuController.leftViewSwipeGestureEnabled = YES;
    }
}

- (UIImageView *)navBGImageView {
    
    if (!_navBGImageView) {
        _navBGImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav_bg"]];
        _navBGImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, kNavigationBarHeight);
    }
    

    return _navBGImageView;
}

- (void)setBarBGAlpha:(CGFloat)barBGAlpha {
    
    self.navBGImageView.alpha = barBGAlpha;
    [self.view bringSubviewToFront:self.navBGImageView];
}

- (void)setDataHasLoad:(BOOL)dataHasLoad {
    
    if (dataHasLoad) {
        self.subScrollView.emptyDataSetSource = self;
        self.subScrollView.emptyDataSetDelegate = self;
    }
}

- (void)rightBarButton:(NSString *)title color:(UIColor *)color selector:(SEL)selector {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:selector];
    barButton.tintColor = color;
    [barButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:15], NSFontAttributeName,nil] forState:(UIControlStateNormal)];
    
    self.navigationItem.rightBarButtonItem = barButton;
}

- (void)rightBarButtonWithImageName:(NSString *)imageName selector:(SEL)selector {
    
    UIImage * image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage: image style:UIBarButtonItemStylePlain target:self action:selector];
    //    barButton.tintColor = HEXCOLOR(0xffffff);
    self.navigationItem.rightBarButtonItem = barButton;
}


- (void)leftBarButton:(NSString *)title  color:(UIColor *)color selector:(SEL)selector {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:selector];
    barButton.tintColor = color;
    self.navigationItem.leftBarButtonItem = barButton;
}

- (void)leftBarButtonWithImageName:(NSString *)imageName selector:(SEL)selector {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:selector];
    //    barButton.tintColor = HEXCOLOR(0xffffff);
    self.navigationItem.leftBarButtonItem = barButton;
}

- (void)leftBarButton1WithImageName:(NSString *)imageName1 selector1:(SEL)selector1 button2ImageName:(NSString *)imageName2 selector2:(SEL)selector2 {
    UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName1] style:UIBarButtonItemStylePlain target:self action:selector1];
    
    barButton1.imageInsets = UIEdgeInsetsMake(0, -8, 0, 8);
    UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName2] style:UIBarButtonItemStylePlain target:self action:selector2];
    barButton2.imageInsets = UIEdgeInsetsMake(0, -20, 0, 20);
    
    self.navigationItem.leftBarButtonItems = @[barButton1,barButton2];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}


- (void)configEmptyDataTableView:(UIScrollView *)subScrollView
                  emptyImageName:(NSString *)imageName
                        describe:(NSString *)describe
                         refresh:(NSString *)btnName
                           block:(void (^) ())block {
    
    self.subScrollView = subScrollView;
    
    self.refreshImageName = imageName;
    self.refreshDescribe = describe;
    self.refreshBtnName = btnName;
    self.refreshBlck = block;
}

- (void)setNavGradient {
    
    UIView * navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kNavigationBarHeight)];
    [self.view addSubview:navView];
    [navView gradientWithColorArray:@[[UIColor colorWithWhite:0 alpha:0.2],[UIColor colorWithWhite:0 alpha:0.01]] frame:navView.bounds isLeftToRight:NO];
    
}

- (YHPopMenuView *)popMenu {
    
    if (!_popMenu) {
        
        CGFloat itemH = 42;
        CGFloat w = 140;
        CGFloat h = 4*itemH + 15;
        CGFloat r = 10;
        CGFloat x = SCREEN_WIDTH - w - r;
        CGFloat y = 2;
        
        YHPopMenuView *popView = [[YHPopMenuView alloc] initWithFrame:CGRectMake(x, y, w, h)];
       popView.itemNameArray = @[@"SITEPLAY_LOOP_MODE_ON",@"SITEPLAY_COMMENT",@"SITEPLAY_FEEDBACK",@"SITEPLAY_BACK_HOME_PAGE"];
        if ([AppManager sharedManager].allPlayWhenListen) {
            
            popView.iconNameArray = @[@"连续播放开",@"写点评",@"意见反馈",@"回到首页"];
        }else {
            popView.iconNameArray = @[@"连续播放关",@"写点评",@"意见反馈",@"回到首页"];
        }
        popView.itemH     = itemH;
        popView.fontSize  = 12.0f;
        popView.fontColor = [UIColor whiteColor];
        
        _popMenu = popView;
    }
    
    return _popMenu;
}

- (void)backBarButtonWithImageName:(NSString *)imageName {
    
    UIImage *image = [UIImage imageNamed:imageName];
    
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationController.navigationBar.backIndicatorImage = image;
    
    self.navigationController.navigationBar.backIndicatorTransitionMaskImage = image;
    
    
//    UINavigationBar * navigationBar = [UINavigationBar appearance];
//    
//    //返回按钮的箭头颜色
//    
////    [navigationBar setTintColor:[UIColor whiteColor]];
//    
//    //设置返回样式图片
//    
//    UIImage *image = [UIImage imageNamed:imageName];
//    
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    
//    navigationBar.backIndicatorImage = image;
//    
//    navigationBar.backIndicatorTransitionMaskImage = image;
    
}

#pragma mark - DZNEmptyDataSetSource Methods

//- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
//
//    NSString *text = @"暂无数据";
//    return [[NSAttributedString alloc] initWithString:text attributes:nil];
//}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *text = [AppManager sharedManager].networkState == AFNetworkReachabilityStatusNotReachable ? MYLocalizedString(@"PUBLIC_NETWORK_ERROR", nil) : self.refreshDescribe;// @"暂无收藏导游\n发现有趣的就收藏吧~";
    UIFont *font = Font(14);
    UIColor *textColor = [UIColor grayColor];
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    
    return kBGColor;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    
    return -kNavigationBarHeight;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    
    return 50;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    
    if ([AppManager sharedManager].networkState == AFNetworkReachabilityStatusNotReachable) {
        return nil;
    }
    
    NSString *text = self.refreshBtnName;
    
    if (text.length == 0) {
        return nil;
    }
    
    UIFont *font = Font(14);
    UIColor *textColor = kThemeColor;
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return [AppManager sharedManager].networkState == AFNetworkReachabilityStatusNotReachable ? [UIImage imageNamed:@"网络不好"] : [UIImage imageNamed:self.refreshImageName];
}

#pragma mark - DZNEmptyDataSetDelegate Methods

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    
    return YES;
}

//- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
//
//    return YES;
//}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    
    return YES;
}

//- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
//
//    IGLog(@"%s",__FUNCTION__);
//}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    
    if (self.refreshBlck) {
        self.refreshBlck();
    }
}


@end
